package com.welove.activity;

import java.util.List;
import java.util.Map;

import com.welove.R;
import com.welove.app.TBaseApplication;
import com.welove.asynctask.AsyncTaskListener;
import com.welove.http.TBaseHttpClient;
import com.welove.util.EtIbUtil;
import com.welove.util.InputManager;
import com.welove.util.InternetUtil;
import com.welove.util.PreferenceUtil;
import com.welove.util.ShowUtil;
import com.welove.util.StringUtil;
import com.welove.widget.TitleBar;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.Gallery;
import android.widget.GridView;
import android.widget.HorizontalScrollView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.ToggleButton;
import android.widget.ViewFlipper;

/**
 * @ClassName: BaseActivity
 * @Description: 所有类的基类
 * @author dreamfutureba@gmail.com
 * @date 2013年11月4日 上午10:19:21
 * 
 */
public abstract class BaseActivity extends FragmentActivity implements
		AsyncTaskListener {
	private static final String TAG=BaseActivity.class.getSimpleName();
	
	protected Context mContext;// 上下文
	protected TitleBar mTitleBar;
	protected LinearLayout mContentLayout;
	
	private ShowUtil showDialog;
	private InternetUtil internetUtil;
	private TBaseHttpClient httpClient;
	private StringUtil stringUtil;
	private PreferenceUtil preferenceUtil;
	private EtIbUtil etIbUtil;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);// 去除标题
		
		initBaseData();
		initTitleBar();
		installViews();
		registerEvents();
	}
	
	@Override
	public void setContentView(int layoutResID) {
		LayoutInflater inflater = LayoutInflater.from(BaseActivity.this);
		inflater.inflate(layoutResID, mContentLayout);
	}

	@Override
	public void setContentView(View view) {
		super.setContentView(view);
	}

	private void initBaseData() {
		mContext = this;
		super.setContentView(R.layout.activity_base);
		mTitleBar = (TitleBar) this.findViewById(R.id.base_titlebar);
		Log.e(TAG, "创建Titlebar");
		mContentLayout = (LinearLayout) this.findViewById(R.id.base_content);
		
		showDialog = new ShowUtil(this);
		internetUtil = TBaseApplication.getInternetUtil();
		httpClient = TBaseApplication.getHttpClient();
		stringUtil = TBaseApplication.getStringUtil();
		preferenceUtil = TBaseApplication.getPreferenceUtil();
		etIbUtil = TBaseApplication.getEtIbUtil();
	}

	/** TitleBar图标或title的更换,显示等操作。 */
	protected void initTitleBar() {
		// 设置左边的按钮的事件
		if (null != mTitleBar && null != mTitleBar.titlebar_btn_left) {
			mTitleBar.titlebar_btn_left.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					finish();
				}
			});
		}
	}
	/** 加载布局文件，初始化对应的view对象。 */
	protected abstract void installViews();
	/** Activity中所有View的绑定事件，都放置到该类中。 */
	protected abstract void registerEvents();

	public Button initBtn(int id) {
		Button btn = (Button) this.findViewById(id);
		return btn;
	}

	public TextView initTv(int id) {
		TextView tv = (TextView) this.findViewById(id);
		return tv;
	}

	public EditText initEt(int id) {
		EditText et = (EditText) this.findViewById(id);
		return et;
	}

	public ImageButton initIb(int id) {
		ImageButton ib = (ImageButton) this.findViewById(id);
		return ib;
	}

	public ImageView initIv(int id) {
		ImageView iv = (ImageView) this.findViewById(id);
		return iv;
	}

	public RadioButton initRb(int id) {
		RadioButton rb = (RadioButton) this.findViewById(id);
		return rb;
	}

	public CheckBox initCb(int id) {
		CheckBox cb = (CheckBox) this.findViewById(id);
		return cb;
	}

	public RadioGroup initRp(int id) {
		RadioGroup rp = (RadioGroup) this.findViewById(id);
		return rp;
	}

	public ProgressBar initPg(int id) {
		ProgressBar pg = (ProgressBar) this.findViewById(id);
		return pg;
	}

	public SeekBar initSb(int id) {
		SeekBar sb = (SeekBar) this.findViewById(id);
		return sb;
	}

	public RatingBar initRt(int id) {
		RatingBar rt = (RatingBar) this.findViewById(id);
		return rt;
	}

	public Spinner initSp(int id) {
		Spinner sp = (Spinner) this.findViewById(id);
		return sp;
	}

	public ListView initLv(int id) {
		ListView lv = (ListView) this.findViewById(id);
		return lv;
	}

	public GridView initGv(int id) {
		GridView gv = (GridView) this.findViewById(id);
		return gv;
	}

	public Gallery initGy(int id) {
		Gallery gy = (Gallery) this.findViewById(id);
		return gy;
	}

	public ToggleButton initTb(int id) {
		ToggleButton tb = (ToggleButton) this.findViewById(id);
		return tb;
	}

	public LinearLayout initLinear(int id) {
		LinearLayout linear = (LinearLayout) this.findViewById(id);
		return linear;
	}

	public RelativeLayout initRelative(int id) {
		RelativeLayout relative = (RelativeLayout) this.findViewById(id);
		return relative;
	}

	public FrameLayout initFrame(int id) {
		FrameLayout frame = (FrameLayout) this.findViewById(id);
		return frame;
	}

	public ScrollView initScroll(int id) {
		ScrollView scroll = (ScrollView) this.findViewById(id);
		return scroll;
	}

	public HorizontalScrollView initHorizScroll(int id) {
		HorizontalScrollView horizscroll = (HorizontalScrollView) this
				.findViewById(id);
		return horizscroll;
	}

	public ViewFlipper initFlipper(int id) {
		ViewFlipper flipper = (ViewFlipper) this.findViewById(id);
		return flipper;
	}

	public View initView(Context context, int id) {
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(id, null);
		return view;
	}

	public Button initBtn(Dialog dlg, int id) {

		Button btn = (Button) dlg.findViewById(id);
		return btn;
	}

	public TextView initTv(Dialog dlg, int id) {
		TextView tv = (TextView) dlg.findViewById(id);
		return tv;
	}

	public EditText initEt(Dialog dlg, int id) {
		EditText et = (EditText) dlg.findViewById(id);
		return et;
	}

	public ImageButton initIb(Dialog dlg, int id) {
		ImageButton ib = (ImageButton) dlg.findViewById(id);
		return ib;
	}

	public ImageView initIv(Dialog dlg, int id) {
		ImageView iv = (ImageView) dlg.findViewById(id);
		return iv;
	}

	public RadioButton initRb(Dialog dlg, int id) {
		RadioButton rb = (RadioButton) dlg.findViewById(id);
		return rb;
	}

	public RadioGroup initRp(Dialog dlg, int id) {
		RadioGroup rp = (RadioGroup) dlg.findViewById(id);
		return rp;
	}

	public CheckBox initCb(Dialog dlg, int id) {
		CheckBox cb = (CheckBox) dlg.findViewById(id);
		return cb;
	}

	public ProgressBar initPg(Dialog dlg, int id) {
		ProgressBar pg = (ProgressBar) dlg.findViewById(id);
		return pg;
	}

	public SeekBar initSb(Dialog dlg, int id) {
		SeekBar sb = (SeekBar) dlg.findViewById(id);
		return sb;
	}

	public Spinner initSp(Dialog dlg, int id) {
		Spinner sp = (Spinner) dlg.findViewById(id);
		return sp;
	}

	public ListView initLv(Dialog dlg, int id) {
		ListView lv = (ListView) dlg.findViewById(id);
		return lv;
	}

	public GridView initGv(Dialog dlg, int id) {
		GridView gv = (GridView) dlg.findViewById(id);
		return gv;
	}

	public Gallery initGy(Dialog dlg, int id) {
		Gallery gy = (Gallery) dlg.findViewById(id);
		return gy;
	}

	public ToggleButton initTb(Dialog dlg, int id) {
		ToggleButton tb = (ToggleButton) dlg.findViewById(id);
		return tb;
	}

	public LinearLayout initLinear(Dialog dlg, int id) {
		LinearLayout linear = (LinearLayout) dlg.findViewById(id);
		return linear;
	}

	public RelativeLayout initRelative(Dialog dlg, int id) {
		RelativeLayout relative = (RelativeLayout) dlg.findViewById(id);
		return relative;
	}

	public FrameLayout initFrame(Dialog dlg, int id) {
		FrameLayout frame = (FrameLayout) dlg.findViewById(id);
		return frame;
	}

	public ScrollView initScroll(Dialog dlg, int id) {
		ScrollView scroll = (ScrollView) dlg.findViewById(id);
		return scroll;
	}

	public HorizontalScrollView initHorizScroll(Dialog dlg, int id) {
		HorizontalScrollView horizscroll = (HorizontalScrollView) dlg
				.findViewById(id);
		return horizscroll;
	}

	public ViewFlipper initFlipper(Dialog dlg, int id) {
		ViewFlipper flipper = (ViewFlipper) dlg.findViewById(id);
		return flipper;
	}

	public Button initBtn(View v, int id) {

		Button btn = (Button) v.findViewById(id);
		return btn;
	}

	public TextView initTv(View v, int id) {
		TextView tv = (TextView) v.findViewById(id);
		return tv;
	}

	public EditText initEt(View v, int id) {
		EditText et = (EditText) v.findViewById(id);
		return et;
	}

	public ImageButton initIb(View v, int id) {
		ImageButton ib = (ImageButton) this.findViewById(id);
		return ib;
	}

	public ImageView initIv(View v, int id) {
		ImageView iv = (ImageView) v.findViewById(id);
		return iv;
	}

	public RadioButton initRb(View v, int id) {
		RadioButton rb = (RadioButton) v.findViewById(id);
		return rb;
	}

	public RadioGroup initRp(View v, int id) {
		RadioGroup rp = (RadioGroup) v.findViewById(id);
		return rp;
	}

	public CheckBox initCb(View v, int id) {
		CheckBox cb = (CheckBox) v.findViewById(id);
		return cb;
	}

	public ProgressBar initPg(View v, int id) {
		ProgressBar pg = (ProgressBar) v.findViewById(id);
		return pg;
	}

	public SeekBar initSb(View v, int id) {
		SeekBar sb = (SeekBar) v.findViewById(id);
		return sb;
	}

	public Spinner initSp(View v, int id) {
		Spinner sp = (Spinner) v.findViewById(id);
		return sp;
	}

	public ListView initLv(View v, int id) {
		ListView lv = (ListView) v.findViewById(id);
		return lv;
	}

	public GridView initGv(View v, int id) {
		GridView gv = (GridView) v.findViewById(id);
		return gv;
	}

	public Gallery initGy(View v, int id) {
		Gallery gy = (Gallery) v.findViewById(id);
		return gy;
	}

	public ToggleButton initTb(View v, int id) {
		ToggleButton tb = (ToggleButton) v.findViewById(id);
		return tb;
	}

	public LinearLayout initLinear(View v, int id) {
		LinearLayout linear = (LinearLayout) v.findViewById(id);
		return linear;
	}

	public RelativeLayout initRelative(View v, int id) {
		RelativeLayout relative = (RelativeLayout) v.findViewById(id);
		return relative;
	}

	public FrameLayout initFrame(View v, int id) {
		FrameLayout frame = (FrameLayout) v.findViewById(id);
		return frame;
	}

	public ScrollView initScroll(View v, int id) {
		ScrollView scroll = (ScrollView) v.findViewById(id);
		return scroll;
	}

	public HorizontalScrollView initHorizScroll(View v, int id) {
		HorizontalScrollView horizscroll = (HorizontalScrollView) v
				.findViewById(id);
		return horizscroll;
	}

	public ViewFlipper initFlipper(View v, int id) {
		ViewFlipper flipper = (ViewFlipper) v.findViewById(id);
		return flipper;
	}

	@Override
	public void onBeforeTask(int taskId) {
		// TODO Auto-generated method stub
	}

	@Override
	public Object onTask(int taskId, Object... params) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onAfterTask(int taskId, Object result) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onTaskError(int taskId, Exception exception) {
		// TODO Auto-generated method stub
	}

	public Dialog createDlg(int style, int dlgview, boolean isTouchCancel,
			int gravity) {
		return showDialog.createDlg(style, dlgview, isTouchCancel, gravity);
	}

	public void showPgDlg(String title, String msg) {
		showDialog.showPgDlg(title, msg);
	}

	public void closePgDlg() {
		showDialog.closePgDlg();
	}

	public void showToast(int flag, String msg) {
		showDialog.showToast(flag, msg);
	}

	public boolean isInternet() {
		return internetUtil.isInternet();
	}

	public boolean isWifi() {
		return internetUtil.isWifi();
	}

	public boolean is3G() {
		return internetUtil.is3G();
	}

	public TBaseHttpClient httpClient() {
		return httpClient;
	}

	public boolean isEmail(String str) {
		int i = stringUtil.checkEmail(str);
		if (i == 0) {

			return false;
		} else if (i == -1) {

			return false;
		} else {
			return true;
		}
	}

	public boolean isPhone(String str) {
		int i = stringUtil.checkPhone(str);
		if (i == 0) {

			return false;
		} else if (i == -1) {

			return false;
		} else {
			return true;
		}
	}

	public boolean isLength(String str, int min, int max) {
		int i = stringUtil.checkLength(str, min, max);
		if (i == 0) {
			return false;
		} else if (i == -1) {
			return false;
		} else {
			return true;
		}
	}

	public String paresStru_str(String str) {
		return stringUtil.parseStr(str);
	}

	public boolean saveStrPref(String filename, String key, String value) {
		preferenceUtil.getPreference(filename);
		return preferenceUtil.saveStrPreference(key, value);
	}

	public String getStrPref(String filename, String key, String defaultStr) {
		preferenceUtil.getPreference(filename);
		return preferenceUtil.getStrPreference(key, defaultStr);
	}

	public boolean saveIntPref(String filename, String key, int value) {
		preferenceUtil.getPreference(filename);
		return preferenceUtil.saveIntPreference(key, value);
	}

	public int getIntPref(String filename, String key) {
		preferenceUtil.getPreference(filename);
		return preferenceUtil.getIntPreference(key);
	}

	public boolean saveFloatPref(String filename, String key, Float value) {
		preferenceUtil.getPreference(filename);
		return preferenceUtil.saveFloatPreference(key, value);
	}

	public float getFloatPref(String filename, String key) {
		preferenceUtil.getPreference(filename);
		return preferenceUtil.getFloatPreference(key);
	}

	public boolean saveLongPref(String filename, String key, Long value) {
		preferenceUtil.getPreference(filename);
		return preferenceUtil.saveLongPreference(key, value);
	}

	public long getLongPref(String filename, String key) {
		preferenceUtil.getPreference(filename);
		return preferenceUtil.getLongPreference(key);
	}

	public boolean saveBoolPref(String filename, String key, boolean value) {
		preferenceUtil.getPreference(filename);
		return preferenceUtil.saveBoolPreference(key, value);
	}

	public boolean getBoolPref(String filename, String key) {
		preferenceUtil.getPreference(filename);
		return preferenceUtil.getBoolPreference(key);
	}

	public boolean saveAllPref(String filename, List<?> list, String... keyName) {
		preferenceUtil.getPreference(filename);
		return preferenceUtil.saveAllPreference(list, keyName);
	}

	public Map<String, ?> getAllPref(String filename) {
		preferenceUtil.getPreference(filename);
		return preferenceUtil.getAllPreference();
	}

	public boolean clearPref(String filename, String key) {
		preferenceUtil.getPreference(filename);
		return preferenceUtil.removeKey(key);
	}

	public boolean clearAllPref(String filename) {
		preferenceUtil.getPreference(filename);
		return preferenceUtil.removeAllKey();
	}

	public void ibChangeText(EditText et, ImageButton ib) {
		etIbUtil.changeIb(et, ib);
	}

	public void closeInput(EditText et) {
		InputManager.closeInput(BaseActivity.this, et);
	}
}
